/**
 *
 */
package com.teamall.web.fpxm.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.teamall.core.common.Page;
import com.teamall.core.convert.IArgConversionService;
import com.teamall.core.idproduce.ISequenceGenerator;
import com.teamall.framework.exception.BaseAppException;
import com.teamall.framework.log.LogManager;
import com.teamall.framework.util.Utils;
import com.teamall.web.fpxm.db.arg.AidProjectArg;
import com.teamall.web.fpxm.db.arg.AidProjectArg.AidProjectCriteria;
import com.teamall.web.fpxm.db.dao.AidProjectDao;
import com.teamall.web.fpxm.db.po.AidProjectPO;
import com.teamall.web.fpxm.service.IAidProjectService;

/**
 * <Description> <br>
 *
 * @author codeCreater<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2014年11月11日 <br>
 * @see com.teamall.web.fpxm.service.impl <br>
 * @since V1.0<br>
 */

@Service("aidProjectService")
public class AidProjectServiceImpl implements IAidProjectService {

    private static final LogManager logger = LogManager
            .getLogger(AidProjectServiceImpl.class);

    @Autowired
    private AidProjectDao aidProjectDao;


    /**
     * 查询条件转换成Arg类的服务接口
     */
    @Resource(name = "defaultArgConversionService")
    private IArgConversionService argConversionService;

    /**
     * 主键生成器
     */
    @Resource(name = "sequenceProcGenerator")
    private ISequenceGenerator sequenceGenerator;


    @Override
    public AidProjectPO selectByPrimaryKey(Integer key) throws BaseAppException {
        // ///////
        // TODO 根据业务场景，设置查询条件、数据校验等

        // ///////
        return aidProjectDao.selectByPrimaryKey(key);
    }

    @Override
    public List<AidProjectPO> selectByArg(AidProjectPO record) throws BaseAppException {
        logger.debug("selectByArg begin...record={0}", record);

        // 第一种方式：自己创建arg，自行设置查询条件及操作符
        AidProjectArg arg = new AidProjectArg();
        AidProjectCriteria criteria = arg.createCriteria();
        if (Utils.notEmpty(record.getProjectType())) {
            criteria.andProjectTypeEqualTo(record.getProjectType());
        }
        if (Utils.notEmpty(record.getState())) {
            criteria.andStateEqualTo(record.getState());
        }
      

        return aidProjectDao.selectByArg(arg);
    }

    @Override
    public Page<AidProjectPO> selectByArgAndPage(AidProjectPO record, Page<AidProjectPO> resultPage)
            throws BaseAppException {
        logger.debug("selectByArgAndPage begin...record={0}", record);

        // 第一种方式：自己创建arg，自行设置查询条件及操作符
         AidProjectArg arg = new AidProjectArg();
        // //TODO 根据业务场景，设置查询条件，示例
        AidProjectArg.AidProjectCriteria criteria = arg.createCriteria();
        if (Utils.notEmpty(record.getProjectName())) {
            criteria.andProjectNameLike(record.getProjectName());
        }
        if (Utils.notEmpty(record.getProjectType())) {
            criteria.andProjectTypeEqualTo(record.getProjectType());
        }
        // 第二种方式：利用arg转换服务，转换出arg，带上查询条件及操作符，
        // 转换后，还可以自行对arg进行设置修改
        resultPage = aidProjectDao.selectByArgAndPage(arg, resultPage);
        return resultPage;
    }

    @Override
    public int add(AidProjectPO record) throws BaseAppException {
        logger.debug("add begin...record={0}", record);

        // ///////
        // TODO 根据业务场景，进行重名校验、设置主键、设置属性默认值等
        // 获取主键
        int pkId = sequenceGenerator.sequenceIntValue("AID_PROJECT",
                "PROJECT_ID");
        record.setProjectId(pkId);
        // ///////
        aidProjectDao.insertSelective(record);
        return pkId;
    }

    @Override
    public int update(AidProjectPO record) throws BaseAppException {
        logger.debug("update begin...record={0}", record);

        // ///////
        // TODO 根据业务场景，进行重名校验、数据有效性校验、设置属性默认值等

        // ///////

        return aidProjectDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int delete(AidProjectPO record) throws BaseAppException {
        logger.debug("delete begin...record={0}", record);

        // ///////
        // TODO 根据业务场景，进行关联性校验、关联删除等

        // ///////

        return aidProjectDao.deleteByPrimaryKey(record.getProjectId());
    }

    /* (non-Javadoc)
     * @see com.teamall.web.fpxm.service.IAidProjectService#updateByPrimaryKey(com.teamall.web.fpxm.db.po.AidProjectPO)
     */
    @Override
    public int updateByPrimaryKey(AidProjectPO record) throws BaseAppException {
        return aidProjectDao.updateByPrimaryKey(record);
    }

}
